#include "stdafx.h"
#include "Action.h"

#include <comdef.h>

/// Step
Step::Step(long lStep, const char* pszDesc, const char* pszCommand)
  : m_lStep(lStep)
{
  memset(m_szDescription, 0, sizeof m_szDescription);
  memset(m_szCommand, 0, sizeof m_szCommand);
  if (pszDesc) {
    strncpy(m_szDescription, pszDesc, sizeof(m_szDescription)-1);
  }
  if (pszCommand) {
    strncpy(m_szCommand, pszCommand, sizeof(m_szCommand)-1);
  }
}

Step::Step(const Step* pOther)
  : m_lStep(0)
{ 
  memset(m_szDescription, 0, sizeof m_szDescription);
  memset(m_szCommand, 0, sizeof m_szCommand);
  if (pOther) {
    m_lStep = pOther->getStepNo();
    if (pOther->getDescription()) {
      strncpy(m_szDescription, pOther->getDescription(), sizeof m_szDescription-1);
    }    
    if (pOther->getCommand()) {
      strncpy(m_szCommand, pOther->getCommand(), sizeof m_szCommand-1);
    }    
  }
}

Step::~Step()
{

}


/// ACTION
Action::Action() 
  : m_pEventScheme(NULL)
  , m_pszXml(NULL)
{
  memset(m_szExec, 0, sizeof m_szExec);
  memset(m_szEventName, 0, sizeof m_szEventName);
  memset(m_szSeqName, 0, sizeof m_szSeqName);
  memset(m_szActionName, 0, sizeof m_szActionName);
  memset(m_szSeverity, 0, sizeof m_szSeverity);
  memset(m_szActionTime, 0, sizeof m_szActionTime);
  memset(m_szEventCategory, 0, sizeof m_szEventCategory);
  
  m_liSteps.clear();
} 

Action::Action(const char* szExec, 
               const char* szEventName, 
               const char* szSeqName, 
               const char* szActionName,
               const char* szSeverity)
  : m_pEventScheme(NULL)
  , m_pszXml(NULL)
{
  memset(m_szExec, 0, sizeof m_szExec);
  memset(m_szEventName, 0, sizeof m_szEventName);
  memset(m_szSeqName, 0, sizeof m_szSeqName);
  memset(m_szActionName, 0, sizeof m_szActionName);
  memset(m_szSeverity, 0, sizeof m_szSeverity);
  memset(m_szActionTime, 0, sizeof m_szActionTime);
  memset(m_szEventCategory, 0, sizeof m_szEventCategory);

  m_liSteps.clear();

  if (szExec) strncpy(m_szExec, szExec, sizeof m_szExec-1);
  if (szEventName) strncpy(m_szEventName, szEventName, sizeof m_szEventName-1);
  if (szSeqName) strncpy(m_szSeqName, szSeqName, sizeof m_szSeqName-1);
  if (szActionName) strncpy(m_szActionName, szActionName, sizeof m_szActionName-1);
  if (szSeverity) strncpy(m_szSeverity, szSeverity, sizeof m_szSeverity-1);
}

Action::Action(const Action * pOther)
  : m_pEventScheme(NULL)
  , m_pszXml(NULL)
{
  memset(m_szExec, 0, sizeof m_szExec);
  memset(m_szEventName, 0, sizeof m_szEventName);
  memset(m_szSeqName, 0, sizeof m_szSeqName);
  memset(m_szActionName, 0, sizeof m_szActionName);
  memset(m_szSeverity, 0, sizeof m_szSeverity);
  memset(m_szActionTime, 0, sizeof m_szActionTime);
  memset(m_szEventCategory, 0, sizeof m_szEventCategory);

  m_liSteps.clear();

  if (pOther->m_szExec) strncpy(m_szExec, pOther->m_szExec, sizeof m_szExec-1);
  if (pOther->m_szEventName) strncpy(m_szEventName, pOther->m_szEventName, sizeof m_szEventName-1);
  if (pOther->m_szSeqName) strncpy(m_szSeqName, pOther->m_szSeqName, sizeof m_szSeqName-1);
  if (pOther->m_szActionName) strncpy(m_szActionName, pOther->m_szActionName, sizeof m_szActionName-1);
  if (pOther->m_szSeverity) strncpy(m_szSeverity, pOther->m_szSeverity, sizeof m_szSeverity-1);
  if (pOther->m_szActionTime) strncpy(m_szActionTime, pOther->m_szActionTime, sizeof m_szActionTime-1);

  std::list<Step*> li = (pOther->m_liSteps);
  std::list<Step*>::iterator ite;
  for (ite = li.begin(); ite != li.end(); ++ite) {
    m_liSteps.push_back(new Step(*ite));
  }
}

void Action::setActionTime(const char* szActionTime)
{
  memset(m_szActionTime, 0, sizeof m_szActionTime);
  if (szActionTime) {
    strncpy(m_szActionTime, szActionTime, sizeof m_szActionTime-1);
  }
}

void Action::setEventCategory(const char* szEventCategory)
{
  memset(m_szEventCategory, 0, sizeof m_szEventCategory);
  if (szEventCategory) {
    strncpy(m_szEventCategory, szEventCategory, sizeof m_szEventCategory-1);
  }
}

/*
Action::~Action()
{
  if (m_pEventScheme) delete m_pEventScheme;
  if (m_pszXml) delete[] m_pszXml;
}
*/

void Action::addStep(Step * pStep)
{
  m_liSteps.push_back(new Step(pStep));
}

void Action::addEvent(EventScheme * pEventScheme)
{
  m_pEventScheme = new EventScheme(pEventScheme);
}

const char* Action::toXml(const char* pszEvent)
{
  _bstr_t xml = "<action>"
                "  <reference><id/></reference>"
                "  <executable>"+_bstr_t(m_szExec)+"</executable>"
                "  <name>"+_bstr_t(this->m_szActionName)+"</name>"
                "  <procedure>";

  std::list<Step*>::iterator ite;
  for (ite = m_liSteps.begin(); ite != m_liSteps.end(); ++ite) {
    xml += 
                "    <step sequence=\""+_bstr_t((*ite)->getStepNo())+"\">"
                "      <description>"+_bstr_t((*ite)->getDescription())+"</description>"
                "      <command>"+_bstr_t((*ite)->getCommand())+"</command>"
                "    </step>";    
  }

  if (NULL == pszEvent) {
    if (m_pEventScheme) {
      xml += 
                  "    <events>"+_bstr_t(this->m_pEventScheme->toXml())+"</events>";
    }
  } else {
      xml += 
                  "    <events>"+_bstr_t(pszEvent)+"</events>";
  }
  xml +=    
                "  </procedure>"
                "</action>";

  m_pszXml = new char[xml.length()+1];
  memset(m_pszXml, 0, xml.length()+1);
  strncpy(m_pszXml, xml, xml.length());
  return m_pszXml;
}